package com.javafactroy.kunlong.codefactroy.service.stringtemplate;


import cn.hutool.core.util.StrUtil;
import com.javafactroy.kunlong.codefactroy.iface.IStringTemplate;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@Service
@Data
public class StringTemplate implements IStringTemplate {

    String text;
    Map<String, Object> params;

    public StringTemplate() {
        params = new LinkedHashMap<>();
    }

    public StringTemplate clear() {
        params.clear();
        return this;
    }

    public StringTemplate putParam(String key, Object value) {
        params.put(StrUtil.format("${{}}",key), value);
        return this;
    }

    public String parse(String text) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            text = StrUtil.replace(text, entry.getKey(), entry.getValue().toString());
        }
        return text;
    }
}
